package com.dd.controller;

import com.dd.entity.Employees;
import com.dd.entity.R;
import com.dd.enums.HttpEnum;
import com.dd.service.IEmployeesService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author dhd
 * @since 2024-10-21
 */
@RestController
@RequestMapping("/employees")
public class EmployeesController {
    @Autowired
    IEmployeesService iEmployeesService;

    @PostMapping
    public R save(@RequestBody @Valid Employees employees) {
        int i = iEmployeesService.save(employees);
        if (i == 0){
            return R.fail(HttpEnum.BAD_REQUEST.getCode(),"新增失败");
        }
        System.out.println(i);
        return R.success(i);
    }

    @DeleteMapping("/{employeeId}")
    public R delete(@PathVariable Integer employeeId) {
        int i = iEmployeesService.delete(employeeId);
        if (i == 0){
            return R.fail(HttpEnum.BAD_REQUEST.getCode(),"删除失败");
        }
            return R.success("删除成功");
    }

    @PutMapping
    public R update(@RequestBody Employees employees) {
        int i = iEmployeesService.update(employees);
        if (i == 0){
            return R.fail(HttpEnum.BAD_REQUEST.getCode(),"编辑失败");
        }
        return R.success("编辑成功");
    }

    @GetMapping("/{id}")
    public R queryById(@PathVariable("id") Integer id) {
        Employees employees = iEmployeesService.queryById(id);
        return R.success(employees);
    }


    @GetMapping("/page")
    public R<PageInfo<Employees>> getPage(String empName, String phoneNumber,Integer pageNum){
        PageInfo<Employees> pageInfo = iEmployeesService.getPage(empName,phoneNumber,pageNum);
        return R.success(pageInfo);
    }
}
